package com.tan.lbs.service.impl;

import java.util.ArrayList;

import com.tan.lbs.dao.GameConditionDAO;
import com.tan.lbs.dao.GameTriggerDAO;
import com.tan.lbs.dao.ItemDAO;
import com.tan.lbs.dao.NPCDAO;
import com.tan.lbs.dao.QuestDAO;
import com.tan.lbs.dao.impl.GameConditionDAOImpl;
import com.tan.lbs.dao.impl.GameTriggerDAOImpl;
import com.tan.lbs.dao.impl.ItemDAOImpl;
import com.tan.lbs.dao.impl.NPCDAOImpl;
import com.tan.lbs.dao.impl.QuestDAOImpl;
import com.tan.lbs.entity.GameCondition;
import com.tan.lbs.entity.GameTrigger;
import com.tan.lbs.service.GameRuleService;

public class GameRuleServiceImpl implements GameRuleService {
	
	GameConditionDAO gameConditionDAO;
	GameTriggerDAO gameTriggerDAO;
	ItemDAO itemDAO;
	NPCDAO npcDAO;
	QuestDAO questDAO;
	
	public GameRuleServiceImpl() {
		gameConditionDAO = new GameConditionDAOImpl();
		gameTriggerDAO  =new GameTriggerDAOImpl();
		itemDAO = new ItemDAOImpl();
		npcDAO = new NPCDAOImpl();
		questDAO = new QuestDAOImpl();
	}

	@Override
	public ArrayList<GameCondition> retrieveConditionList(String triggerID) {
		 ArrayList<GameCondition> conditions = gameConditionDAO.searchByGameTrigger(triggerID);
		 for(GameCondition condition: conditions) {
			 //This is where the type code and literal changes happens
			 if("itempick".equalsIgnoreCase(condition.getConditionType())) {
				 condition.setParamX(itemDAO.readByID(condition.getParamX()).getTitle());
				 condition.setConditionType("Player has Item X");
			 } else if("npcchat".equalsIgnoreCase(condition.getConditionType())) {
				 condition.setParamX(npcDAO.readByID(condition.getParamX()).getName());
				 condition.setConditionType("Player has finished dialog with NPC X");
			 } else if("itemnum".equalsIgnoreCase(condition.getConditionType())) {
				 condition.setConditionType("Player has Item with same Title X with quantity Y");
			 } else if("npcgreet".equalsIgnoreCase(condition.getConditionType())) {
				 condition.setParamX(npcDAO.readByID(condition.getParamX()).getName());
				 condition.setConditionType("Player is greeting with NPC X");
			 } else if("queststart".equalsIgnoreCase(condition.getConditionType())) {
				 condition.setParamX(questDAO.readByID(condition.getParamX()).getTitle());
				 condition.setConditionType("Player has started Quest X");
			 } else if("questcomplete".equalsIgnoreCase(condition.getConditionType())) {
				 condition.setParamX(questDAO.readByID(condition.getParamX()).getTitle());
				 condition.setConditionType("Player has completed Quest X");
			 } 
		 }
		return conditions;
	}
	
	@Override
	public  GameTrigger getGameObjectTrigger(GameTrigger gameTrigger) {
		GameTrigger readGameTrigger = gameTriggerDAO.readByObjectAndTypeAndGame(gameTrigger.getActionObjectID(),
				gameTrigger.getActionType(),gameTrigger.getGameID());
		//lazy creating: create a new trigger if there's no trigger
		//for the object and the action type
		if(readGameTrigger != null) return readGameTrigger;
		else {
			return gameTriggerDAO.readByID(gameTriggerDAO.create(gameTrigger));
		}
	}

	@Override
	public String addConditionForTrigger(GameCondition condition) {
		return gameConditionDAO.create(condition);
	}

	@Override
	public void modifyConditionForTrigger(GameCondition condition) {
		gameConditionDAO.update(condition);
	}

	@Override
	public void deleteConditionForTrigger(String conditionID) {
		gameConditionDAO.delete(conditionID);
	}

	@Override
	public void modifyTriggerDetails(GameTrigger gameTrigger) {
		GameTrigger modGameTrigger  = gameTriggerDAO.readByID(gameTrigger.getTriggerID());
		modGameTrigger.setTextAfterTrigger(gameTrigger.getTextAfterTrigger());
		gameTriggerDAO.update(modGameTrigger);
	}

	@Override
	public void deleteAllTriggersAndConditions(String gameObjectID) {
		gameConditionDAO.deleteByGameObject(gameObjectID);
		gameTriggerDAO.deleteByGameObject(gameObjectID);	
	}



}
